/*
    SPI interface, three pins mode
 */
#ifndef _SPI_H
#define _SPI_H

#include <stdint.h>
#include <stdbool.h>

/******************************************************************************
                         SPI Debug Configuration
 ******************************************************************************/

/******************************************************************************
                          SPI Configuration
 ******************************************************************************/

/******************************************************************************
                          Parameter Definition
 ******************************************************************************/
/*
   - NRF_SPI0
   - NRF_SPI1
*/
typedef enum
{
    NRF5x_SPI0,
    NRF5x_SPI1,
    NRF5x_SPI2
} spi_periph_t;

/*
    - SPI_FREQUENCY_FREQUENCY_K125  //125kbps.
    - SPI_FREQUENCY_FREQUENCY_K250  //250kbps.
    - SPI_FREQUENCY_FREQUENCY_K500  //500kbps.
    - SPI_FREQUENCY_FREQUENCY_M1    //1Mbps.
    - SPI_FREQUENCY_FREQUENCY_M2    //2Mbps.
    - SPI_FREQUENCY_FREQUENCY_M4    //4Mbps.
    - SPI_FREQUENCY_FREQUENCY_M8    //8Mbps.
 */
typedef enum
{
    SPI_FREQ_125K,
    SPI_FREQ_250K,
    SPI_FREQ_500K,
    SPI_FREQ_1M,
    SPI_FREQ_2M,
    SPI_FREQ_4M,
    SPI_FREQ_8M
} spi_freq_t;

/*
      SPI modes CPOL CPHA
    - SPI_MODE0  0    0
    - SPI_MODE1  0    1
    - SPI_MODE2  1    0
    - SPI_MODE3  1    1
*/
typedef enum
{
    SPI_CPOL_0_CPHA_0,
    SPI_CPOL_0_CPHA_1,
    SPI_CPOL_1_CPHA_0,
    SPI_CPOL_1_CPHA_1
} spi_mode_t;

/*
    - SPI_ORDER_LSB
    - SPI_ORDER_MSB
*/
typedef enum
{
    SPI_ORDER_LSB,
    SPI_ORDER_MSB
} spi_order_t;

typedef void(*dma_int_cb_t);

typedef struct
{
    spi_periph_t spix;
    spi_freq_t spi_freq;
    spi_mode_t spi_mode;
    spi_order_t spi_order;

    bool use_dma;
    dma_int_cb_t dma_int_cb;

    uint8_t scl_pin;
    uint8_t miso_pin;
    uint8_t mosi_pin;
} spi_instance_t;

/******************************************************************************
                          Function Declaration
 ******************************************************************************/
int spi_init(const spi_instance_t *inst);
int spi_off(const spi_instance_t *inst);
int spi_tx_rx(const spi_instance_t *inst, uint8_t *p_tx_buf, uint8_t *p_rx_buf, int len);

#endif
